package 面试;


//import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//n个台阶，一次只能有一步或两步，共有几种走法？
public class test01 {
//    @Test
    public void test(){
        long t1 = System.currentTimeMillis();
        System.out.println(f(40));
        long t2 = System.currentTimeMillis();
        System.out.println("递归用时：" + (t2 - t1));
        long t3 = System.currentTimeMillis();
        System.out.println(loop(40));
        long t4 = System.currentTimeMillis();
        System.out.println("循环迭代用时：" + (t4 - t3));
    }

    //递归
    public int f(int n){
        if(n < 1){
            throw new IllegalArgumentException(n + "不能小于1");
        }
        if(n==1 || n==2){
            return n;
        }
        return f(n-2) + f(n-1);
    }
    //循环迭代
    public int loop(int n){
        if(n < 1){
            throw new IllegalArgumentException(n + "不能小于1");
        }
        if(n==1 || n==2){
            return n;
        }
        int one = 2;
        int two = 1;
        int sum = 0;

        for (int i = 3; i <= n; i++) {
            sum = one + two;
            two = one;
            one = sum;
        }
        return sum;
    }

//    @Test
    public void test02(){
        List<Integer> list = Arrays.asList(1,2,3,4,5);
        list.forEach(System.out::print);
    }

}
